#!/bin/sh /etc/rc.common
# Copyright (c) 2011-2015 OpenWrt.org

START=90

get_config()
{
	config_get_bool enabled $1 enabled 0
	config_get autoactivate $1 autoactivate 1
}

start(){
  config_load vlmcsd
	config_foreach get_config vlmcsd
	[ $enabled -eq 0 ] && exit 0
  /usr/bin/vlmcsd -i /etc/vlmcsd.ini -L 0.0.0.0:1688
  
  HOSTNAME=`uci get system.@system[0].hostname`
	host_name=$(awk -F ',' '/^[ \t]*srv-host=_vlmcs\._tcp/{print $2}' /etc/dnsmasq.conf)
	
	echo $HOSTNAME
	echo $host_name
	
  if [ "$HOSTNAME" != "$host_name" ];then
				sed -i '/^[ \t]*srv-host=_vlmcs\._tcp/d' /etc/dnsmasq.conf
				sed -i '$a\srv-host=_vlmcs\._tcp,'"$HOSTNAME"','"1688"',0,100' /etc/dnsmasq.conf
				/etc/init.d/dnsmasq restart
  fi
	
	echo "KMS Server has started."
}

stop(){
		pid=`pgrep /usr/bin/vlmcsd`
		kill -9 $pid
		echo "KMS Server has stopped."
}

